<?php
if(!in_array(User::getType(), array('leerling', 'docent')))
{
	require ROOT . 'paginas/errors/403.php';
}

function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
    $sort_col = array();
    foreach ($arr as $key=> $row) {
        $sort_col[$key] = $row[$col];
    }

    array_multisort($sort_col, $dir, $arr);
}

require ROOT . 'template/header.php';
echo '<div id="content2"><h1>Top 10 klassen van jouw school!</h1>';

$class = mysql_fetch_assoc(mysql_query("SELECT `school_id` FROM `klassen` WHERE `id`='" . User::getClassID() . "'"));

$klassenSQL = mysql_query("SELECT * FROM `klassen` WHERE `school_id`='" . $class['school_id'] . "' ORDER BY gouden_sterren DESC, zilveren_sterren DESC, bronzen_sterren DESC");
$klassen = array();

while($klas = mysql_fetch_assoc($klassenSQL))
{
	$klas['score'] = $klas['gouden_sterren'] * 100 + $klas['zilveren_sterren'] * 75 + $klas['bronzen_sterren'] * 50;
	$klassen[] = $klas;
}
array_sort_by_column($klassen, 'score', SORT_DESC);


echo '<table><tr><th>Naam klas</th><th>Score</th><th>Gouden Sterren:</th><th>Zilveren sterren:</th><th>Bronzen sterren:</th></tr>';

$max = (count($klassen) > 10) ? 10 : count($klassen);

for($i = 0; $i < $max; $i++)
{
	$klas = $klassen[$i];
	echo '<tr>
		<td>' . $klas['naam'] . '</td>
		<td>' . $klas['score'] . '</td>
		<td>' . $klas['gouden_sterren'] . '</td>
		<td>' . $klas['zilveren_sterren'] . '</td>
		<td>' . $klas['bronzen_sterren'] . '</td>
	</tr>';
}


echo '</table><br /><p>Iedere gouden ster is <strong>100</strong> punten waard, iedere zilveren ster <strong>75</strong> en bronze sterren zijn <Strong>50</strong> punten waard.</p></div>';
require ROOT . 'template/footer.php';